package com.lai.laiojcodesandbox.utils;

import cn.hutool.core.util.StrUtil;
import com.lai.laiojcodesandbox.model.ExecuteMessage;
import org.springframework.util.StopWatch;

import java.io.*;

/**
 * 进程工具类
 */
public class ProcessUtils {

    /**
     * 执行进程并获取信息
     *
     * @param runProcess
     * @param opName
     * @return
     */
    public static ExecuteMessage runProcessAndGetMessage(Process runProcess, String opName) {
        ExecuteMessage executeMessage = new ExecuteMessage();
        try {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            // 等待程序执行，获取错误码
            int exitValue = runProcess.waitFor();
            //获取程序执行时间
            stopWatch.stop();
            executeMessage.setTime(stopWatch.getLastTaskTimeMillis());

            executeMessage.setExitValue(exitValue);

            // 正常退出
            if (exitValue == 0) {
                System.out.println(opName + "成功");
                // 分批获取进程的正常输出
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
                StringBuilder complieOutputStringBuilder = new StringBuilder();
                // 逐行获取
                String complieOutputLine;
                while ((complieOutputLine = bufferedReader.readLine()) != null) {
                    complieOutputStringBuilder.append(complieOutputLine);
                }
                executeMessage.setMessage(complieOutputStringBuilder.toString());
            } else {
                // 异常退出
                System.out.println(opName + "失败，错误码：" + exitValue);
                // 分批获取进程的正常输出
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
                StringBuilder complieOutputStringBuilder = new StringBuilder();
                // 逐行获取
                String complieOutputLine;
                while ((complieOutputLine = bufferedReader.readLine()) != null) {
                    complieOutputStringBuilder.append(complieOutputLine);
                }
                executeMessage.setMessage(complieOutputStringBuilder.toString());

                // 分批获取进程的输出
                BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(runProcess.getErrorStream()));
                StringBuilder errorComplieOutputStringBuilder = new StringBuilder();
                // 逐行获取
                String errorcomplieOutputLine;
                while ((errorcomplieOutputLine = errorBufferedReader.readLine()) != null) {
                    errorComplieOutputStringBuilder.append(errorcomplieOutputLine);
                }
                executeMessage.setErrorMessage(errorComplieOutputStringBuilder.toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return executeMessage;
    }

    /*public static ExecuteMessage runInteractProcessAndGetMessage(Process runProcess, String opName, String args) {
        ExecuteMessage executeMessage = new ExecuteMessage();
        try {
            // 向控制台输入程序
            OutputStream outputStream = runProcess.getOutputStream();

            // 5-1:35:00
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
            String[] s = args.split(" ");
            String argString = StrUtil.join("\n", s) + "\n";
            outputStreamWriter.write(argString);
            //相当于按了回车，执行输入的发送
            outputStreamWriter.flush();

            InputStream inputStream = runProcess.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder complieOutputStringBuilder = new StringBuilder();
            // 逐行获取
            String complieOutputLine;
            while ((complieOutputLine = bufferedReader.readLine()) != null) {
                complieOutputStringBuilder.append(complieOutputLine);
            }
            executeMessage.setMessage(complieOutputStringBuilder.toString());

            outputStream.close();
            outputStreamWriter.close();
            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return executeMessage;
    }*/
}
